# encoding: utf-8

import wsgiref.handlers
import os
import re
import cgi
import datetime
import sys

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import memcache

import admin
import models
import functions
import cache
import options
import smaider
import settings
import taggable

class SiteXml(admin.BaseRequestHandler):
    """/admin/categories - Admin page for categories."""
    def get(self):
        str =u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
                <head><meta http-equiv=Content-Type content="text/html;charset=utf-8"></head><body>'''
        baseUrl='http://' + self.request.host + '/'
        
        posts = models.Post.all();
        for post in posts:
            url = baseUrl+ "post/"+post.permalink
            str = str +'''<a href="''' + url +'''.html">'''+ url +'''</a><br/>\n'''
        str=str+"<br/>\n" 
        
        cats = models.Category.all().order('category_name');
        for cat in cats:
            url = baseUrl+"category/"+cat.category_url
            str = str +'''<a href="''' + url +'''/">'''+ url +'''</a><br/>\n'''
        str=str+"<br/>\n"   
        
        tags = taggable.Tag.Get_Tags_By_Frequency();
        for tag in tags:
            if len(tag.tagged) > 0:
                url = baseUrl+"tag/"+tag.tag
                str = str +'''<a href="''' + url +'''/">'''+ url +'''</a><br/>\n'''
        str=str+"<br/>\n"
        
        arcs = models.Archive.all().order('yearmonth')
        for arc in arcs:
            url = baseUrl+"archive/"+arc.yearmonth+"\n"
            str = str +'''<a href="''' + url +'''/">'''+ url +'''</a><br/>\n'''
        
        str =str+'''</body>
            </html>'''
        self.response.headers["Content-Type"] = "text/html"
        self.response.out.write(str)
        
def getplugurl():
    return {1:('i',re.compile('/sitemap.html*$'),SiteXml)}
